<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <style type="text/css">
        .layui-table-cell{
            text-align: center;
            height: auto;
            white-space: normal;
        }
        .layui-table img{
            max-width: 179px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset" style="padding-top: 3px;height: 65px">
            <legend>搜索信息</legend>
            <div style="margin: 2px 10px 10px 10px">
                <form id="form-search" class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <span class="layui-breadcrumb" lay-separator="  ">
                              <a style="color: #00b7ee">状态</a>
                              <a href="">全部</a>
                              <a href="">公开</a>
                              <a href="">私密</a>
                            </span>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <select name="colId"  lay-search="">
                                    <option value="">请选择文章类型</option>
                                    <option value="0">全部</option>
                                    <option value="1">原创</option>
                                    <option value="2">转载</option>
                                    <option value="3">翻译</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <select name="colId"  lay-search="">
                                    <option value="">请选择分类</option>
                                    <option value="0">全部</option>
                                    <option value="1">原创</option>
                                    <option value="2">转载</option>
                                    <option value="3">翻译</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <select name="colId"  lay-search="">
                                    <option value="">请选择标签</option>
                                    <option value="0">全部</option>
                                    <option value="1">原创</option>
                                    <option value="2">转载</option>
                                    <option value="3">翻译</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" id="title" name="articleTitle" placeholder="请输入文章名" autocomplete="off" class="layui-input" value="">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="btn-search" type="submit" class="layui-btn layui-btn-normal layui-btn-radius"  lay-submit lay-filter="data-search-btn">搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="getCheckData">批量删除</button>
                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">批量导出</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="isAll">批量导入</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="img">
            <img src="{{d.articleCover}}" width="179px" height="100px" style="border-radius: 5px"/>
        </script>
        <script type="text/html" id="tag">
            {{# layui.each(d.tags, function(index, item){     }}
            <button class="layui-btn layui-btn-primary layui-border-green layui-btn-sm" >{{item.tagName}}</button>
            {{# });  }}
        </script>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script>
    function refreshTable(){
        $("#form-search").reset;
        $("#btn-search").click();

    }

    layui.use(['form', 'table', 'laytpl'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laytpl = layui.laytpl;
        table.render({
            elem: '#currentTableId',
            url: '/admin/articles',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: 'checkbox',width: 30}
                    ,{field:'articleCover', title:'文章封面', width:210, templet:"#img", unresize:true}
                    ,{field:'articleTitle', title:'标题', width:140}
                    ,{field:'categoryName', title:'分类', width:120}
                    ,{field:'tag', title:'标签', width:175, templet: "#tag"}
                    ,{field:'type', title:'类型', width:100, templet: function (data) {
                        if (data.type == 1){data.type = '<button class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm" >原创</button>';return data.type;}
                        else if (data.type == 2){data.type = '<button class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm" >转载</button>';return data.type;}
                        else {data.type = '<button class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm" >翻译</button>';return data.type;}
                    }}
                    ,{field:'createTime', title:'发表时间', width: 173, sort: true, templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}</div>"}
                    ,{field:'isTop', title:'置顶', width:88, templet: function (data) {
                        if (data.isTop == 0){return '<div id="'+data.id+'"><input type="checkbox" name="close" lay-filter="top" value=' + data.id + ' lay-skin="switch" lay-text="ON|OFF"></div>'}
                        else if (data.isTop == 1) {return '<div id="'+data.id+'"></div><input type="checkbox" checked="checked" name="close" lay-filter="top" value=' + data.id + ' lay-skin="switch" lay-text="ON|OFF"></div>'}
                    } }
                    ,{field:'isHot', title:'推荐', width:88, templet: function (data) {
                            if (data.isHot == 0){return '<div id="hot'+data.id+'"><input type="checkbox" name="close" lay-filter="hot" value=' + data.id + ' lay-skin="switch" lay-text="ON|OFF"></div>'}
                            else if (data.isHot == 1) {return '<div id="hot'+data.id+'"></div><input type="checkbox" checked="checked" name="close" lay-filter="hot" value=' + data.id + ' lay-skin="switch" lay-text="ON|OFF"></div>'}
                    }}
                    ,{title:'操作', toolbar: '#barDemo', width:146}
                ]
            ],
            limits: [10, 15, 20, 25],
            limit: 10,
            page: true,
            even: true
        });

        form.on('switch(top)', function (data) {
            var isTop = this.checked?"1":"0";
            var id = data.value;
            var url = '/admin/top';
            res = {
                'id' : id,
                'isTop' : isTop
            }
            $.post(url,res,function (response) {
                layer.tips(response.message,'#'+res.id+'',{
                    tips: [1, '#3595CC'],
                    time: 4000,
                    anim: 6
                });
            })
        })

        form.on('switch(hot)', function (data) {
            var isHot = this.checked?"1":"0";
            var id = data.value;
            var url = '/admin/hot';
            res = {
                'id' : id,
                'isHot' : isHot
            }
            $.post(url,res,function (response) {
                layer.tips(response.message,'#hot'+res.id+'',{
                    tips: [1, '#b554cb'],
                    time: 4000,
                    anim: 6
                });
            })
        })

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            data = data.field
            console.log(data)
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    // searchParams: result
                    stuId : data.stuId,
                    name : data.name,
                    colId : data.colId
                }
            });
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加学生',
                    type: 2,
                    shade: 0.2,
                    maxmin:false,
                    shadeClose: true,
                    skin: 'layui-layer-molv',
                    area: ['100%', '100%'],
                    content: '/toAdd',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if(data.length == 0 ){
                    layer.alert('未选中任何数据', {icon: 2,anim:6,title:"警告" });
                    return false;
                }
                var idArr = new Array();
                $(data).each(function () {
                    idArr.push(this.stuId);
                });
                console.log(idArr)
                layer.confirm('您确认删除选中信息？', {icon: 3, title:'删除确认'}, function(index){
                    // 异步请求删除数据、结果提示、刷新表格
                    var url = "stu/" + idArr.join(",");
                    var params = {
                        _method:"DELETE"
                    }
                    $.get(url,params,function (response) {
                        if(response.code == 0){
                            layer.msg(response.message, {icon: 1});
                            refreshTable();
                        }else{
                            layer.alert(response.message, {icon: 2,anim:6 });
                        }

                    });

                    layer.close(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    shadeClose: true,
                    skin: 'layui-layer-lan',
                    area: ['40%', '90%'],
                    content: '/stu/'+data.stuId,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('您确认删除该员工：'+data.name + " ？", {icon: 3, title:'删除确认'}, function(index){
                    // 异步请求删除数据、结果提示、刷新表格
                    var url = "stu/" + data.stuId;
                    var params = {
                        _method:"DELETE"
                    }
                    $.post(url,params,function (response) {
                        if(response.code == 0){
                            layer.msg(response.message, {icon: 1});
                            refreshTable();
                        }else{
                            layer.alert(response.message, {icon: 2,anim:6 });
                        }

                    });

                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>